﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jxym.wlkc2011.Module.Default.model;
using Jxym.DBUtility;
using System.Reflection;
using System.Data.SqlClient;
using Jxym.Common.MyFile;

namespace Jxym.wlkc2011.Module.Default.server.Files
{
    public partial class FilesAdd : System.Web.UI.Page
    {
        //public class files2:model.Files
        //{
        //    private string pathNow;

        //    public string PathNow
        //    {
        //        get { return pathNow; }
        //        set { pathNow = value; }
        //    } 
        //}
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                UpLoad upload = new UpLoad();
                upload.AllowExts = "bmp|chm|csv|dll|eml|ttf|gif|hlp|html|ini|java|jpg|jpeg|mdb|mp3|rmvb|mp4|mpeg|rm|xls|xlsx|ppt|pptx|doc|docx|pdf|pps|psd|rar|rtf|shareFolder|sql|swf|sys|txt|vsd|wab|wps|xml|";
                upload.IsExtClass = true;
                upload.Save();
                string[] filenames = upload.FileNames.Split('|');

                
                MsSqlDal<model.Files> ifiles = new MsSqlDal<model.Files>();
                PropertyInfo[] fields = typeof(model.Files).GetProperties();
                List<string> listName = new List<string>();
                List<string> listValue = new List<string>();
                object identity = 0;
                int textid = 0;
                int modelid = 0;

                if (Request.Params["modelid"] != null) modelid = Convert.ToInt32(Request.Params["modelid"]);
                string returnStr = "{'success':false,'msg':'参数错误'}";

                foreach (PropertyInfo property in fields)
                {
                    if (Request.Params[property.Name] != null)
                    {
                        listName.Add("@" + property.Name);
                        listValue.Add(Request.Params[property.Name]);
                    }
                }

                if (upload.FileNames == "" && Request.Params["PathNow"] == null)
                {
                    listName.Remove("@Path");
                    listValue.Remove(Request.Params["Path"]);

                    //listName.Add("@PathNow");
                    //listValue.Add(upload.FileNames);
                    //listName.Add("@Size");
                    //listValue.Add(upload.FileSizes);
                    //listName.Add("@Path");
                    //listValue.Add(upload.FileNames);
                    //listName.Add("@Type");
                    //listValue.Add(upload.GetExts);
                }
                if (upload.FileNames != "")
                {
                    listName.Remove("@PathNow");
                    listValue.Remove(Request.Params["PathNow"]);
                    listName.Remove("@Type");
                    listValue.Remove(Request.Params["Type"]);
                    listName.Remove("@Size");
                    listValue.Remove(Request.Params["Size"]);

                    listName.Add("@PathNow");
                    listValue.Add(upload.FileNames);
                    listName.Add("@Size");
                    listValue.Add(upload.FileSizes);
                    listName.Add("@Path");
                    listValue.Add(upload.FileNames);
                    listName.Add("@Type");
                    listValue.Add(upload.GetExts);

                }
                if (upload.FileNames == "" && Request.Params["PathNow"] != null)
                {
                    listName.Add("@Path");
                    listValue.Add(Request.Params["PathNow"]);
                }
                listName.Add("@CreateID");
                listValue.Add(Jxym.Common.Request.AdminID("user"));

                SqlParameter[] parameters = new SqlParameter[listName.Count];

                for (int i = 0; i < listName.Count; i++)
                {
                    parameters[i] = new SqlParameter(listName[i], listValue[i]);
                }

                if (Request.Params["ID"] != null)
                {
                    textid = Convert.ToInt32(Request.Params["ID"]);
                }

                if (textid > 0)
                {
                    model.Files fi = ifiles.GetModel("ID=" + textid, null);
                    string Err = upload.ErrorStr;
                    if (Err != "")
                    {
                        returnStr = "{'success':false,'msg':'" + Err + "'}";
                    }
                    else
                    {
                        //判断该通知发布人是不是当前用户 和 当前用户权限
                        if (MyGet.GetPermissionsValidation(modelid,"U", Jxym.Common.Request.AdminID("user")) && Jxym.Common.Request.AdminID("user").Equals(fi.CreateID))
                        {
                            identity = ifiles.Updata("ID=" + textid, parameters);
                            returnStr = "{'success':true,'msg':'更新完成'}";
                        }
                        else
                        {
                            returnStr = "{'success':false,'msg':'对不起，您没有编辑权限'}";
                        }
                    }
                }
                else
                {
                    string Err = upload.ErrorStr;
                    if (Err != "")
                    {
                        returnStr = "{'success':false,'msg':'" + Err + "'}";
                    }
                    else
                    {
                        if (MyGet.GetPermissionsValidation(modelid,"A", Jxym.Common.Request.AdminID("user")))
                        {
                            identity = ifiles.Add(parameters);
                            returnStr = "{'success':true,'msg':'添加完成'}";
                        }
                        else
                        {
                            returnStr = "{'success':false,'msg':'对不起，您没有添加权限'}";
                        }

                    }
                }

                Response.Write(returnStr);


                //string Err = upload.ErrorStr;
                //if (Err != "")
                //    jsonStr = "{'success':false,'msg':'" + Err + "'}";
                //else
                //    jsonStr = "{'success':true,'count':'" + filenames.Length + "'}";
                //Response.Write(jsonStr);
            }
        }
    }
}